"""The application's Globals object"""
from pylons import config



articlecsvpath = 'articles.csv'

class Globals(object):
    """Globals acts as a container for objects available throughout the
    life of the application
    """

    def __init__(self):
        """One instance of Globals is created during application
        initialization and is available during requests via the 'g'
        variable
        """
        print 'creating globals...'
        
        from os.path import exists        
        if not exists(articlecsvpath): return {},[]
        
        import csv
        reader = csv.reader(open(articlecsvpath, "rb"))
        
        # article dict
        from sys import getfilesystemencoding
        fse = getfilesystemencoding()
        d = {}
        for row in reader:
            key = row[0]
            if key.find('/') >= 0: continue
            
            # convert to utf-8
            if not isinstance(key, str):
                key = key.decode(fse).encode('utf-8')

            d[key] = row[1]
        self.article_dict = d
        
        keys = d.keys()
        keys = map(lambda x:x.decode('utf-8'),keys)

        # random article links
        from random import shuffle
        shuffle(keys)        
        link = lambda x: '<li><a href="/article/%s">%s</a></li>' % (x.replace(' ', '_'),x)
        self.random_articles = map(link, keys)
        
        # search
        keys.sort()
        from string import lower
        self.search_lower = map(lower, keys)
        self.search_links = map(link, keys)
        

